package com.jdy.haoduoaiteacher.adapter;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.activity.BaseActivity;
import com.jdy.haoduoaiteacher.model.CartoonVo;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

@SuppressLint("InflateParams")
public class SelectionAdapter extends BaseAdapter {

  private final BaseActivity mContext;
  private final LayoutInflater mlInflater;
  private final DisplayImageOptions options;
  private final ArrayList<CartoonVo> mList;

  public SelectionAdapter(BaseActivity context, ArrayList<CartoonVo> mList) {
    this.mContext = context;
    this.mList = mList;
    mlInflater = LayoutInflater.from(mContext);

    // 用 displayImage 加载图片 的 配置参数
    options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.default_img_listitem) // 下载 失败 显示的 图片
        .cacheInMemory(true) // 缓存
        .bitmapConfig(Bitmap.Config.RGB_565) // 图片格式
        .build();
    // 创建默认的ImageLoader配置参数
    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
        mContext).writeDebugLogs() // 打印 log 信息
        .build();
    ImageLoader.getInstance().init(configuration);
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  @Override
  public Object getItem(int position) {
    return mList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    CartoonVo listVO = mList.get(position);
    ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mlInflater.inflate(R.layout.choice_item, null);
      holder.itemImg = (ImageView) convertView.findViewById(R.id.screenshot);
      holder.tagicon = (ImageView) convertView.findViewById(R.id.tagicon);
      holder.itemName = (TextView) convertView.findViewById(R.id.title);
      holder.Views = (TextView) convertView.findViewById(R.id.views);
      holder.updatestates = (TextView) convertView.findViewById(R.id.episode);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    // 为 控件 填 参数
    holder.itemImg.setScaleType(ScaleType.CENTER_CROP);
    ImageLoader.getInstance().displayImage(listVO.cover, holder.itemImg,
        options);
    if (listVO.corner_icon.equals("hot")) {
     // holder.tagicon.setBackgroundResource(R.drawable.hot_corner);
    }
    holder.itemName.setText(listVO.name);
    holder.updatestates.setText("共" + listVO.total + "集");
    holder.Views.setText(listVO.followers + "");
    return convertView;
  }

  private static class ViewHolder {
    private ImageView itemImg;
    private ImageView tagicon;
    private TextView itemName;
    private TextView updatestates;
    private TextView Views;
  }
}
